package person.wend.javalearnexample.util;

import java.util.*;

public class FormatterExample {
    public static void main(String[] args) {
        // formatter使用示例
        formatterTest();
    }

    public static void formatterTest(){
        // 创建一个Formatter对象，输出到StringBuffer
        StringBuffer sb = new StringBuffer();
        Formatter formatter = new Formatter(sb, Locale.CHINESE);

        // 示例1：格式化整数和浮点数
        int intValue = 42;
        double doubleValue = 3.1415926535;
        formatter.format("Integer: %d, Float: %.2f%n", intValue, doubleValue);
        System.out.println(sb.toString());
        sb.setLength(0); // 清空StringBuffer以便重用

        // 示例2：左对齐和右对齐
        String leftAligned = "left";
        String rightAligned = "right";
        formatter.format("Left aligned: %-10s | Right aligned: %10s%n", leftAligned, rightAligned);
        System.out.println(sb.toString());
        sb.setLength(0);

        // 示例3：带宽度和填充
        int number = 7;
        formatter.format("Number with padding: %05d%n", number);
        System.out.println(sb.toString());
        sb.setLength(0);

        // 示例4：格式化日期
        Date currentDate = new Date();
        formatter.format("Current date and time: %tc%n", currentDate);
        System.out.println(sb.toString());

        // 关闭Formatter
        formatter.close();

        // 使用String.format()
        String formattedString = String.format("Formatted string: %s", "example");
        System.out.println(formattedString);

        // 使用System.out.printf()直接打印格式化字符串
        System.out.printf("Direct print: %s%n", "formatted output");
    }



    public static void formatterBuilderTest(){

    }
}